package org.example;

import java.awt.*;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.io.DataInputStream;
import java.io.EOFException;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;

public class Server {
    private static final int PORT = 8888;
    private static final float SENSITIVITY = 1.8f;

    public static void main(String[] args) throws Exception {
        ServerSocket serverSocket = new ServerSocket(PORT);
        System.out.println("Server running on port " + PORT);

        while (true) {
            Socket client = serverSocket.accept();
            client.setTcpNoDelay(true);
            new Thread(() -> handleClient(client)).start();
        }
    }
    private static void handleClient(Socket client) {
        try (DataInputStream in = new DataInputStream(client.getInputStream())) {
            Robot robot = new Robot(); // 移出try-with-resources，单独创建
            System.out.println("Client connected: " + client.getInetAddress());

            while (true) {
                byte cmd = in.readByte();
                switch (cmd) {
                    case 0: // 移动
                        float dx = in.readFloat();
                        float dy = in.readFloat();
                        Point p = MouseInfo.getPointerInfo().getLocation();
                        robot.mouseMove(
                                (int) (p.x + dx * SENSITIVITY),
                                (int) (p.y + dy * SENSITIVITY)
                        );
                        break;

                    case 1: // 左键按下
                        robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
                        break;

                    case 2: // 左键释放
                        robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
                        break;

                    case 3: // 右键按下
                        robot.mousePress(InputEvent.BUTTON3_DOWN_MASK);
                        break;

                    case 4: // 右键释放
                        robot.mouseRelease(InputEvent.BUTTON3_DOWN_MASK);
                        break;

                    case 5: // 处理文本输入
                        String text = in.readUTF();
                        for (char c : text.toCharArray()) {
                            int code = KeyEvent.getExtendedKeyCodeForChar(c);
                            if (Character.isUpperCase(c)) {
                                robot.keyPress(KeyEvent.VK_SHIFT);
                                robot.keyPress(code);
                                robot.keyRelease(code);
                                robot.keyRelease(KeyEvent.VK_SHIFT);
                            } else if (code != KeyEvent.VK_UNDEFINED) {
                                robot.keyPress(code);
                                robot.keyRelease(code);
                            }
                        }
                        break;
                }
            }
        } catch (SocketException | EOFException e) {
            System.out.println("Client disconnected: " + e.getMessage());
        } catch (Exception e) {
            System.out.println("Error: " + e.getMessage());
        } finally {
            try {
                client.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}